package cn.tedu.filter;

import cn.tedu.domain.User;
import cn.tedu.service.UserService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URLDecoder;

public class AutoLoginFilter implements Filter {

    /**
     * Spring容器
     */
    private WebApplicationContext context = null;

    @Override
    public void init(FilterConfig filterConfig) {
        //获取Spring容器
        ServletContext sc = filterConfig.getServletContext();
        context = WebApplicationContextUtils.getWebApplicationContext(sc);
    }


    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
        try {
            HttpServletRequest req = (HttpServletRequest) request;
            //未登录
            User user = (User) req.getSession().getAttribute("user");
            if (user == null) {
                //有自动登录cookie
                Cookie[] cs = req.getCookies();
                Cookie findC = null;
                if (cs != null) {
                    for (Cookie c : cs) {
                        if ("autologin".equals(c.getName())) {
                            findC = c;
                            break;
                        }
                    }
                }
                if (findC != null) {
                    //自动登录cookie中用户名密码正确
                    String username = URLDecoder.decode(findC.getValue().split("#")[0], "utf-8");
                    String password = findC.getValue().split("#")[1];
                    UserService userService = context.getBean(UserService.class);
                    User fuser = userService.loginUser(username, password);
                    if (fuser != null) {
                        //自动登录
                        req.getSession().setAttribute("user", fuser);
                    }
                }
            }

            //放行访问
            chain.doFilter(request, response);
        } catch (IOException ioException) {
            System.out.println(ioException.getMessage());
            ioException.printStackTrace();
        } catch (ServletException servletException) {
            System.out.println(servletException.getMessage());
            servletException.printStackTrace();
        }
    }

}
